package jmind.base.algo;

/**
 * description:
 *
 * @author weibo.xie
 * @date:2021/10/26 上午10:17
 */
public class Maths {

    public static void main(String[] args) {
        int x=1001 ;
        for(int i=1;i<1000;i++){
             if(sumx3(i)!=sumx2(i)){
                 System.err.println(i);
             }
        }

        System.err.println("over");
    }

    //  求x的平方根 的整数部分， 牛顿迭代
    public static  int sqrtInt(int x){
        return (int) sqrt(x/2,x);
    }
    public static double sqrt(double i ,int x){
        double res=(i+x/i)/2 ;
        if(res==i){
            return i;
        }
        return  sqrt(res,x);
    }


    public static double sqrt(double c) {
        if (c < 0) {
            return Double.NaN;
        }

        double e = 1e-15;
        double x = c;
        double y = (x + c / x) / 2;
        while (Math.abs(x - y) > e) {
            x = y;
            y = (x + c / x) / 2;
        }
        return x;
    }
    // 1+2+3+……+n=n（n+1）/2
    public static  long sumx(int x){
     return  (long) x*(x+1) /2;
    }

    public static  long sumx2(int x){
        long sum=0;
        for(int i=1;i<=x;i++){
            sum+= i ;
        }
        return  sum ;
    }


    public static long sumx3(int n){
        if(n % 2 == 0){//偶数
            return (long)((1 + n))*(n / 2);
        }else{//奇数
            return (long)((1 + n))*(n / 2) + (n / 2 + 1);
        }
    }
}
